<?php

namespace App\Service\Fee\Import;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class ImportSpecialAnnualFeeOrderTime
{

    public int $total = 0;

    public int $success = 0;

    public function collection(Collection $collection, ?Command $command)
    {
        $collection->first()->map(function ($item, $row) use ($command){
            $row = $row + 1;
            if ($row == 1) {
                return null;
            }
            // 常规内容校验
            list($shopCode, $value) = $item;

            if ($shopCode == null) {
                $command->info('循环次数' . $this->total);
                $command->info('成功次数' . $this->success);
                dd('执行结束');
            }
            //金额校验

            //根据shopCode获取shopId

            $signOrderModel = OpenShopSignOrderModel::query()->where('shopCode', $shopCode)
                ->where('status', '!=', SignOrderEnum::ORDER_STATUS_NO)
                ->with(['mainProcessRel'])
                ->first();

            if (null == $signOrderModel) {
                return null;
            }

            if (null == $signOrderModel->mainProcessRel) {
                return null;
            }
            ++$this->total;
            if ($signOrderModel->mainProcessRel->startFeeTime) {
                $availableTime = strtotime($signOrderModel->mainProcessRel->startFeeTime);
            } else {
                //没有设定年费生成时间
                //找到开业时间
                $availableTime = (int)OpenShopStartBusinessModel::query()->where('mainProcessId', $signOrderModel->mainProcessRel->id)->value('startBusinessTime');
            }
            $signOrderModel->mainProcessRel->startFeeTime = date('Ymd', strtotime('+' . $value . 'day', $availableTime));
            $signOrderModel->mainProcessRel->save();
            ++$this->success;
            $command->info('门店Code为-' . $shopCode . '的年费订单过期时间修改成功');
        })->filter();
    }
}
